<?php

namespace tkan\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use tkan\models\FondCollection;
use tkan\models\FondCollectionSearch;
use tkan\models\FondItem;
use tkan\models\FondItemSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * NewsController implements the CRUD actions for News model.
 */
class CollectionController extends Controller
{

    /**
     * Lists all News models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new NewsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $this->layout = 'common';

        return $this->render('index', [
            'searchModel' => $searchModel,
            'newsDataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single News model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionCollectionView($id)
    {
        $collectionModel = $this->findModel($id);

        $searchModel = new FondItemSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query->andWhere(['id_fond_collection' => $collectionModel->id]);

        $this->layout = 'common';
        
        return $this->render('view', [
            'dataProvider' => $dataProvider,
            'collectionModel' => $collectionModel,
        ]);
    }

    /**
     * Displays a single News model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionItemView($id, $itemId)
    {
        $collectionModel = $this->findModel($id);
        $itemModel = $this->findItemModel($itemId);

        $this->layout = 'common';
        
        return $this->render('viewItem', [
            'collectionModel' => $collectionModel,
            'itemModel' => $itemModel,
        ]);
    }

    /**
     * Finds the News model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return News the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = FondCollection::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * Finds the News model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return News the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findItemModel($id)
    {
        if (($model = FondItem::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
